/*
 * 功能: 贝勒德事件
 * From: 芬芬时尚潮流
 */

var infomap;
//第三阶段倒计时 (单位:分钟)
var phaseTime = 1;
//自定义复活次数
var reviveCount = 5;

function init() {
	em.setProperty("state", "0");
	em.setProperty("leader", "true");
}

function setup(level, partyid) {
	infomap = new Map();
	em.setProperty("state", "1");
	em.setProperty("leader", "true");

	var eim = em.newInstance("BeidlerPQ");
	infomap.put("clearType", "0");
	infomap.put(String(863010100), "0");
	eim.setProperty("open", "1");
	eim.setProperty("BeidlerLArm", "0");
	eim.setProperty("BeidlerRArm", "0");
	eim.setProperty("BeidlerHip", "0");
	eim.setProperty("BeidlerHead", "0");
	eim.setProperty("Check", "1");
	eim.setProperty("Core", "0");
	eim.setProperty("clearType", "0");
	eim.setProperty("reward", "0");
	eim.setInstanceMap(863010100).resetFully();
	eim.setInstanceMap(863010500).resetFully(); //testDummyBigboss
	eim.setInstanceMap(863010420).resetFully();
	eim.setInstanceMap(863010410).resetFully();
	eim.setInstanceMap(863010400).resetFully();
	eim.setInstanceMap(863010320).resetFully();
	eim.setInstanceMap(863010310).resetFully();
	eim.setInstanceMap(863010300).resetFully();
	eim.setInstanceMap(863010230).resetFully();
	eim.setInstanceMap(863010220).resetFully();
	eim.setInstanceMap(863010210).resetFully();
	eim.setInstanceMap(863010200).resetFully();

	//BOSS
	//eim.setInstanceMap(863010430).resetFully();//BeidlerLArm  9390611  84 70
	//eim.setInstanceMap(863010330).resetFully();//BeidlerRArm  9390610 3 68
	//eim.setInstanceMap(863010240).resetFully();//BeidlerHip  9390612  13 87

	var map2 = eim.setInstanceMap(863010430);
	map2.resetFully();
	var LArm = em.getMonster(9390611);
	LArm.handleDeadBound(1);
	map2.spawnMonsterOnGroundBelow(LArm, new java.awt.Point(84, 69));

	var map3 = eim.setInstanceMap(863010330);
	map3.resetFully();
	var RArm = em.getMonster(9390610);
	RArm.handleDeadBound(1);
	map3.spawnMonsterOnGroundBelow(RArm, new java.awt.Point(1, 69));

	var map4 = eim.setInstanceMap(863010240);
	map4.resetFully();
	var Hip = em.getMonster(9390612);
	Hip.handleDeadBound(1);
	map4.spawnMonsterOnGroundBelow(Hip, new java.awt.Point(3, 69));
	eim.setInstanceMap(863010700).resetFully();
	var map1 = eim.setInstanceMap(863010600); //BeidlerHead  9390600
	map1.resetFully();
	eim.getMapFactoryMap(863010600).killAllMonsters(false);
	eim.startEventTimer(1000 * 60 * 30); // 30 min
	return eim;
}

function playerEntry(eim, player) {
	for (var i = 0; i < eim.getPlayerCount(); i++) {}
	if (i <= 1) {
		eim.setProperty("Name", "[ 困 难 ] 贝 勒 德");
		eim.setProperty("PlayerName", eim.getPlayers().get(0).getName());
	}
	var map = eim.getMapInstance(0);
	if (player.isAdmin()) {
		reviveCount = 99;
	}
	player.setReviveCount(reviveCount);
	player.changeMap(map, map.getPortal(0));
}

function playerRevive(eim, player) {
	if (player.getReviveCount() > 0) {
		var map = player.getMap();
		player.eventRevive(map, map.getPortal(0));
		return true;
	}
	player.addHP(50);
	var map = eim.getMapFactoryMap(863000100);
	player.changeMap(map, map.getPortal(0));
	return true;
}

function changedMap(eim, player, mapid) {

	switch (mapid) {
		case 863010600: //贝勒德的头  16
			var smap = eim.getMapInstance(16);
			var s = 0;
			if (eim.getProperty("BeidlerHead").equals("0")) {
				eim.setProperty("BeidlerHead", "1");
				s = parseInt(eim.getProperty("BeidlerRArm")) + parseInt(eim.getProperty("BeidlerLArm")) + parseInt(eim.getProperty("BeidlerHip"));
				switch (s) {
					case 0:
						eim.setProperty("Core", "3");
						eim.setProperty("rate", "2000");
						eim.setProperty("nlevel", "220");
						break;
					case 1:
						eim.setProperty("Core", "2");
						eim.setProperty("rate", "500");
						eim.setProperty("nlevel", "195");
						break;
					case 2:
						eim.setProperty("Core", "1");
						eim.setProperty("rate", "50");
						eim.setProperty("nlevel", "175");
						break;
					case 3:
						eim.setProperty("Core", "0");
						eim.setProperty("rate", "1");
						eim.setProperty("nlevel", "150");
						break;
				}
				var mob = em.getMonster(9390600); //贝勒德的头
				mob.disableDrops();
				mob.getStats().setChange(true);
				mob.changeLevelmod(parseInt(eim.getProperty("nlevel")), parseInt(eim.getProperty("rate")));
				switch (s) {
					case 0:
						mob.getChangedStats().setOHp(300000000000); //3000e
						mob.setHp(55000000000);
						break;
					case 1:
						mob.getChangedStats().setOHp(16500000000); //165e
						mob.setHp(16500000000);
						break;
					case 2:
						mob.getChangedStats().setOHp(3100000000); //31e
						mob.setHp(3100000000);
						break;
					case 3:
						mob.getChangedStats().setOHp(60000000); //6000w
						mob.setHp(60000000);
						break;
				}
				eim.registerMonster(mob);
				mob.handleDeadBound(1);
				smap.spawnMonsterOnGroundBelow(mob, new java.awt.Point(7, 61));
			}
			break;
		case 863010240: //贝勒德的肚脐   9390612
		case 863010430: //贝勒德的左手肩膀
		case 863010330: //贝勒德的右手肩膀
		case 863010210: //贝勒德右腿上层
		case 863010310: //贝勒德西部悬崖上方
		case 863010400: //贝勒德东部悬崖下方
		case 863010300: //贝勒德西部悬崖下方
		case 863010200: //贝勒德右腿下层
		case 863010220: //贝勒德左腿下层
		case 863010230: //贝勒德左腿上层
		case 863010320: //贝勒德的右手上臂
		case 863010410: //贝勒德东部悬崖上方
		case 863010420: //贝勒德的左手上臂
		case 863010700: //贝勒德的心脏
		case 863010500: //贝勒德的心脏
			eim.setProperty("open", "2");
		case 863010100: //通往贝勒德的路
			break;
		default:
			eim.unregisterPlayer(player);
			if (eim.disposeIfPlayerBelow(0, 0)) {
				player.restReviveCount();
				em.setProperty("state", "0");
				em.setProperty("leader", "true");
			}
			return;
	}
	if (eim.getProperty(String(mapid)) == null) {
		eim.setProperty(String(mapid), "1");
	}
	infomap.put(String(mapid), "0");
	eim.setInfoStats(infomap.objects);
}

function playerDisconnected(eim, player) {
	return 0;
}

function allGainItem(eim, itemId, maxQ, mixQ) {
	var itemName = "低级贝勒德币";
	if (itemId === 4310097) {
		itemName = "贝勒德币";
	}
	for (var i = 0; i < eim.getPlayerCount(); i++) {
		var itemq = mixQ + Math.floor(Math.random() * maxQ);
		var player = eim.getPlayers().get(i);
		player.gainItem(itemId, itemq, "贝勒德副本");
		player.dropSpouseMessage(0x06, "获取其他物品（" + itemName + "）" + itemq + "个");
	}
}

function monsterValue(eim, mobId) { //杀死怪物
	var core = parseInt(eim.getProperty("Core"));

	switch (mobId) {
		case 9390610:
			eim.setProperty("BeidlerRArm", "1");
			// allGainItem(eim, 4310097, 2, 0);
			// allGainItem(eim, 4310098, 50, 0);
			break;
		case 9390611:
			eim.setProperty("BeidlerLArm", "1");
			// allGainItem(eim, 4310097, 2, 0);
			// allGainItem(eim, 4310098, 50, 0);
			break;
		case 9390612:
			eim.setProperty("BeidlerHip", "1");
			// allGainItem(eim, 4310097, 2, 0);
			// allGainItem(eim, 4310098, 50, 0);
			break;
		case 9390600: // 一阶段
		case 9390601: // 二阶段
		case 9390602: // 三阶段
			allGainItem(eim, 4310097, 3, core);
			allGainItem(eim, 4310098, 50, 0);
			break;
	}

	var hp1 = 300000000000;	// 3000e
	var exp1 = 200000000;   // 2e
	var hp2 = 300000000000;  // 3000e
	var exp2 = 200000000;   // 2e
	switch (core) {
		case 0: // 0核
			hp1 = 60000000; // 6000w
			exp1 = 5000000; // 500w
			hp2 = 5000000; // 500w
			exp2 = 5000000; // 500w
			break;
		case 1: // 1核
			hp1 = 3100000000; // 31e
			exp1 = 20000000;  // 2000w
			hp2 = 50000000;  // 5000w
			exp2 = 20000000;  // 2000w
			break;
		case 2: // 2核
			hp1 = 16500000000; // 165e
			exp1 = 100000000;  // 1e
			hp2 = 300000000;  // 3e
			exp2 = 100000000;  // 1e
			break;
		case 3: // 3核
			break;
	}
	if (mobId == 9390600) {
		eim.setProperty("BeidlerHead", "2"); //第二阶段
		mobid = 9390601; //BL1
		mob = em.getMonster(mobid);
		modified = em.newMonsterStats();
		modified.setOHp(hp1);
		modified.setOMp(mob.getMobMaxMp() * 200);
		modified.setOExp(exp1);
		mob.getStats().setAcc(500000);
		mob.getStats().setLevel(220);
		mob.getStats().setAcc(500000);
		mob.setOverrideStats(modified);
		mob.getStats().setChange(true); // 让修改生效，否则虽然你那样设置了，召唤出来还是默认属性*/
		eim.registerMonster(mob);
		var mapForMob = eim.getMapInstance(863010600);
		mapForMob.spawnMonsterOnGroundBelow(mob, new java.awt.Point(7, 61));
	}
	if (mobId == 9390601) {
		eim.setProperty("BeidlerHead", "3"); //第三阶段
		mobid = 9390602; //BL2
		mob = em.getMonster(mobid);
		modified = em.newMonsterStats();
		modified.setOHp(hp2); //20E 400000000000
		modified.setOMp(mob.getMobMaxMp() * 200);
		modified.setOExp(exp2);
		mob.getStats().setAcc(500000);
		mob.getStats().setLevel(220);
		mob.getStats().setAcc(500000);
		mob.setOverrideStats(modified);
		mob.getStats().setChange(true); // 让修改生效，否则虽然你那样设置了，召唤出来还是默认属性*/
		eim.registerMonster(mob);
		var mapForMob = eim.getMapInstance(863010600);
		mapForMob.spawnMonsterOnGroundBelow(mob, new java.awt.Point(7, 61));
		//设置第三阶段倒计时   1分钟时间
		eim.setRemainingTimer(phaseTime * 60 * 1000);
	}
	if (mobId == 9390602) {
		eim.stopEventCountDown();
		var tomap = eim.getMapInstance(15);
		for (i = 0; i < eim.getPlayers().size(); i++) {
			eim.getPlayers().get(i).changeMap(tomap, tomap.getPortal(0));
		}
		eim.restartEventTimer(5 * 60 * 1000);
		return;
	}
	return 1;
}

function playerExit(eim, player) {
	eim.unregisterPlayer(player);
	player.restReviveCount();
	if (eim.disposeIfPlayerBelow(0, 0)) {
		em.getProperties().clear();
		em.setProperty("state", "0");
		em.setProperty("leader", "true");
	}
}

function scheduledTimeout(eim) {
	eim.disposeIfPlayerBelow(100, 863000100);
	em.getProperties().clear();
	em.setProperty("state", "0");
	em.setProperty("leader", "true");
}

function scheduledCountdownOut(eim) {
	eim.disposeIfPlayerBelow(100, 863000100);
	em.getProperties().clear();
	em.setProperty("state", "0");
	em.setProperty("leader", "true");
}

function clearPQ(eim) {
	scheduledTimeout(eim);
}

function allMonstersDead(eim) {}

function leftParty(eim, player) {
	eim.disposeIfPlayerBelow(100, 863000100);
}

function disbandParty(eim) {
	eim.disposeIfPlayerBelow(100, 863000100);
}

function playerDead(eim, player) {}

function cancelSchedule() {}

function pickUpItem(eim, player, itemID) {
	// if (itemID == 4310097) {
	// 	//要调单次多少个币请在这里修改！规则：玩家获得贝勒德币总数：（0~2随机数）+1 千万不要修改贝勒德币的交易属性！否则此处会造成刷币问题
	// 	var itemq = Math.floor(Math.random() * 3);
	// 	player.gainItem(itemID, itemq, "贝勒德副本");
	// 	player.dropSpouseMessage(0x06, "额外获取其他物品（贝勒德币）" + itemq + "个");
	// }
	// if (itemID == 4310097 || itemID == 4310098) {
	// 	for (var i = 0; i < eim.getPlayerCount(); i++) {
	// 		eim.getPlayers().get(i).gainItem(itemID, 1, "贝勒德副本");
	// 	}
	// }
}

function monsterDrop(eim, player, mob) {}

Map = function() {
	this.objects = new Object();

	// 加入元素
	this.put = function(key, value) {
		this.objects[key] = value;
	};

	// 删除元素
	this.remove = function(key) {
		this.objects[key] = undefined;
	};

	// 是否存在某键值
	this.containsKey = function(key) {
		return this.objects[key] ? true : false;
	};

	// 获取某元素
	this.get = function(key) {
		return this.objects[key];
	};

	// 是否存在某值
	this.containsValue = function(value) {
		for (var temp in this.objects) {
			if (this.objects[temp] == value) {
				return true;
			}
		}
		return false;
	};

	// 集合大小
	this.size = function() {
		var counter = 0;
		for (var temp in this.objects) {
			counter++;
		}
		return counter;
	};
};